package com.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.entity.Commodity;
import com.entity.Order;
import com.service.CommodityService;
import com.service.OrderService;
import com.util.ReturnResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/api")
public class OrderController {
    @Resource
    private OrderService service;

    @RequestMapping("/order/selectList/{page}/{limit}")
    @ResponseBody
    public ReturnResult selectList(@PathVariable("page") int page, @PathVariable("limit") int limit){
        ReturnResult result = new ReturnResult();
        Page<Order> pageConfig = new Page<>(page,limit);
        pageConfig.setTotal(service.selectCount(null));
        Page<Order> commodityPage = service.selectPage(pageConfig,null);
        result.returnSuccess(commodityPage,"查询成功！");
        return result;
    }
    
    @RequestMapping("/order/selectOne/{id}")
    @ResponseBody
    public ReturnResult selectOne(@PathVariable("id") int id){
        ReturnResult result = new ReturnResult();
        Order order = service.selectById(id);
        result.returnSuccess(order,"查询成功！");
        return result;
    }

    @RequestMapping("/order/add")
    @ResponseBody
    public ReturnResult add(@RequestBody Order order){
        ReturnResult result = new ReturnResult();
        boolean result_Add = service.insert(order);
        if(result_Add)
            result.returnSuccess(result_Add,"新增成功！");
        else
            result.returnFail("新增失败！");
        return result;
    }

    @RequestMapping("/order/upd")
    @ResponseBody
    public ReturnResult upd(@RequestBody Order order){
        ReturnResult result = new ReturnResult();
        boolean result_Upd = service.updateById(order);
        if(result_Upd)
            result.returnSuccess(result_Upd,"修改成功！");
        else
            result.returnFail("修改失败");
        return result;
    }

    @RequestMapping("/order/del/{id}")
    @ResponseBody
    public ReturnResult del(@PathVariable("id") int id){
        ReturnResult result = new ReturnResult();
        boolean result_Del = service.deleteById(id);
        if(result_Del)
            result.returnSuccess(result_Del,"删除成功！");
        else
            result.returnFail("删除失败！");
        return result;
    }
}
